/*
 * Created on Feb 25, 2005
 */
package com.scalar.chiptrack.finance.openpo.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.actioncommands.PurchaseOrderReceiptCommandFactoryAction;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * @author Ravikanth
**/
public class PurchaseOrderReceiptAction extends ChipTrackExtendedAction
{
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception	
	{
		HashMap errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info    = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String action   = request.getParameter( REQUEST_ACTION );
		ActionForward actionForward	= mapping.findForward( "receiptSearch" );
		ChipTrackExtendedAction actionCommand = null;
		
		if( errors == null ){errors = new HashMap();}
		if( info == null ){	info = new HashMap();}
		
		actionCommand = PurchaseOrderReceiptCommandFactoryAction.getActionCommand( action );
		
		if( actionCommand == null )
		{
			Logger.error( ERROR_COULD_NOT_CREATE_OPEN_PURCHASE_ORDER_INSTANCE );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_CREATE_OPEN_PURCHASE_ORDER_INSTANCE );
			request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
			request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
			request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
			actionForward = mapping.findForward( "receiptSearch" );
		}
		else
		{
			try
			{
				actionForward = actionCommand.execute( mapping, form, request, response );
				errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
				info    = ( HashMap ) request.getAttribute( INFO_MESSAGES );
			}
			catch ( Exception e )
			{
				Logger.error( ERROR_WHILE_EXECUTING_OPEN_PURCHASE_ORDER_ACTION_EXECUTE, e );
				errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_WHILE_EXECUTING_OPEN_PURCHASE_ORDER_ACTION_EXECUTE );
				pSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
				request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
				request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
				request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
				actionForward = mapping.findForward( "receiptSearch" );
			}
		}
		request.setAttribute( INFO_MESSAGES, info );	
		request.setAttribute( ERROR_MESSAGES, errors );
		
		return actionForward;
	}
}